package com.xueyi.operations.controller.admin;

import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.security.utils.SecurityUtils;
import com.xueyi.operations.domain.dto.OpFaultAcceptanceDto;
import com.xueyi.operations.domain.dto.OpFaultCloseDto;
import com.xueyi.operations.domain.dto.OpFaultResultDto;
import com.xueyi.operations.domain.enums.FaultProcessEnum;
import com.xueyi.operations.service.IOpFaultAcceptanceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import com.xueyi.operations.controller.base.BOpFaultProcessController;
import com.xueyi.operations.domain.dto.OpFaultProcessDto;
import com.xueyi.operations.domain.query.OpFaultProcessQuery;
import io.swagger.v3.oas.annotations.Operation;
import com.xueyi.common.security.annotation.AdminAuth;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 流程处理管理 | 管理端 业务处理
 *
 * @author lwb
 */
@AdminAuth
@RestController
@Tag(name = "流程处理 | 管理端 业务处理")
@RequestMapping("/admin/faultprocess")
public class AOpFaultProcessController extends BOpFaultProcessController {


    @Override
    @GetMapping("/list")
    @Operation(summary = "查询流程处理列表")
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:list')")
    public AjaxResult list(OpFaultProcessQuery opFaultProcess) {
        return super.list(opFaultProcess);
    }

    @Override
    @GetMapping(value = "/{id}")
    @Operation(summary = "查询流程处理详细")
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:single')")
    public AjaxResult getInfo(@PathVariable Serializable id) {
        return super.baseService.getAllInfo(id);
//        return super.getInfo(id);
    }

    @Override
    @PostMapping("/export")
    @Operation(summary = "流程处理导出")
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:export')")
    @Log(title = "流程处理管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, OpFaultProcessQuery opFaultProcess) {
        super.export(response, opFaultProcess);
    }

    @Override
    @PostMapping
    @Operation(summary = "流程处理新增")
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:add')")
    @Log(title = "流程处理管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody OpFaultProcessDto opFaultProcess) {
        opFaultProcess.setFaultProcessStatus(FaultProcessEnum.UNACCEPTED);
        return super.add(opFaultProcess);
    }

    @Override
    @PutMapping
    @Operation(summary = "流程处理修改")
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:edit')")
    @Log(title = "流程处理管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody OpFaultProcessDto opFaultProcess) {
        return super.edit(opFaultProcess);
    }

    @Override
    @PutMapping("/status")
    @Operation(summary = "流程处理修改状态")
    @PreAuthorize("@ss.hasAnyAuthority('RD:faultprocess:process:edit', 'RD:faultprocess:process:es')")
    @Log(title = "流程处理管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody OpFaultProcessDto opFaultProcess) {
        return super.editStatus(opFaultProcess);
    }

    @Override
    @DeleteMapping("/batch/{idList}")
    @Operation(summary = "流程处理批量删除")
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:del')")
    @Log(title = "流程处理管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    @PutMapping("/dispose")
    @Operation(summary = "流程处理受理")
    @Log(title = "流程处理管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:dispose')")
    public AjaxResult dispose(@RequestBody OpFaultAcceptanceDto opFaultAcceptanceDto){
        opFaultAcceptanceDto.setAcceptorUser(SecurityUtils.getUserId());
        if(StringUtils.isEmpty(opFaultAcceptanceDto.getAcceptorUserName())){
            opFaultAcceptanceDto.setAcceptorUserName(SecurityUtils.getUserName());
        }
        return super.baseService.dispose(opFaultAcceptanceDto);
    }

    @PutMapping("/disposeResult")
    @Operation(summary = "受理结果")
    @Log(title = "流程处理受理结果", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:disposeresult')")
    public AjaxResult disposeResult(@RequestBody OpFaultResultDto opFaultResultDto){
        opFaultResultDto.setAcceptorUser(SecurityUtils.getUserId());
        if(StringUtils.isEmpty(opFaultResultDto.getAcceptorUserName())){
            opFaultResultDto.setAcceptorUserName(SecurityUtils.getUserName());
        }
        return super.baseService.disposeResult(opFaultResultDto);
    }

    @PutMapping("/disposeClose")
    @Operation(summary = "工单关闭")
    @Log(title = "工单关闭", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasAuthority('RD:faultprocess:process:disposeclose')")
    public AjaxResult disposeClose(@RequestBody OpFaultCloseDto opFaultCloseDto){
        opFaultCloseDto.setAcceptorUser(SecurityUtils.getUserId());
        if(StringUtils.isEmpty(opFaultCloseDto.getAcceptorUserName())){
            opFaultCloseDto.setAcceptorUserName(SecurityUtils.getUserName());
        }
        return super.baseService.disposeClose(opFaultCloseDto);
    }

}
